<template>
	<view class="page">
		<scroll-view class="scrollList" scroll-y :scroll-into-view="scrollViewId" :style="{height:winHeight+'px'}">
			<view class="tui-list search-result">
				<view class="tui-list-cell" hover-class="tui-list-cell-hover" v-for="(item,index) in searchResult" :key="index"
				 @tap="selectCity" :data-name="item" :hover-stay-time='150'>
					<view class="tui-list-cell-navigate">
						<view class="content">
							<view class="title">{{item.name}}</view>
							<view class="sub-title">{{item.tel}}</view>
						</view>
						<image :src="'../../static/images/classify/7.png'" class="img2" @click="_doCall(item)"></image>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	const cityData = require('../../utils/index.list.js')
	export default {
		data() {
			return {
				lists: [],
				touchmove: false, // 是否在索引表上滑动
				touchmoveIndex: -1,
				titleHeight: 0, // A字距离窗口顶部的高度
				indexBarHeight: 0, // 索引表高度
				indexBarItemHeight: 0, // 索引表子项的高度
				scrollViewId: '', // scroll-view滚动到的子元素的id
				winHeight: 0,
				inputShowed: false, // 输入框是否显示
				inputVal: '', // 搜索框输入的内容
				searchResult: [] // 搜索结果
			}
		},
		computed:{
			userInfo(){
				let us = {}
				try{
					us = uni.getStorageSync(this.java110Constant.mapping.USER_INFO);
					us = this.java110Util.des.desDecrypt(us)
					us = JSON.parse(us)
				}catch(e){
					//TODO handle the exception
				}
				return us
			}
		},
		onLoad: function(options) {
			const that = this;
			// let _objData = {
			// 	page: 1,
			// 	row: 15,
			// 	communityId:this.userInfo.communityId
			// };
			// console.log(this.userInfo)
			// this.java110Context.request({
			// 	url:that.java110Constant.url.listStores,
			// 	header: that.java110Context.getHeaders(),
			// 	method: "GET",
			// 	data: _objData, //动态数据
			// 	success: function(res) {
			// 		console.log("请求返回信息：", res);
			// 		if (res.statusCode == 200) {
			// 			let _data = res.data;
			// 			let _staffs = [];
			// 			console.log('list',_data);
						that.searchCity();
			// 			return;
			// 		}
			// 		uni.showToast({
			// 			title: "服务器异常了",
			// 			icon: 'none',
			// 			duration: 2000
			// 		})
			// 	},
			// 	fail: function(e) {
			// 		uni.showToast({
			// 			title: "服务器异常了",
			// 			icon: 'none',
			// 			duration: 2000
			// 		})
			// 	}
			// })
			
			setTimeout(() => {
				uni.getSystemInfo({
					success: function(res) {
						let winHeight = res.windowHeight;
						let barHeight = winHeight - uni.upx2px(204);
						that.winHeight = winHeight;
						that.indexBarHeight = barHeight;
						that.indexBarItemHeight = barHeight / 25;
						that.titleHeight = uni.upx2px(128);
						that.lists = cityData.list
					}
				})
			}, 50)
		},
		methods: {
			clearInput() {
				this.inputVal = "";
				this.inputShowed = false;
				this.searchResult = [];
				uni.hideKeyboard() //强行隐藏键盘
			},
			inputTyping(e) {
				this.inputVal = e.detail.value
			},
			_loadStaffInfo: function() {
				//获取商户ID
				let _that = this;
				// let _userInfo = this.java110Context.getUserInfo();
				// let storeId = this.userInfo.storeId;
				// console.log(this.userInfo)
				let _objData = {
					page: 1,
					row: 5,
					communityId: this.userInfo.communityId,
					memberTypeCd: '390001200002'
				};
				_that.java110Context.request({
					url: _that.java110Constant.url.listStore,
					header: _that.java110Context.getHeaders(),
					method: "GET",
					data: _objData,
					//动态数据
					success: function(res) {
						console.log("请求返回信息：", res);
						if (res.statusCode == 200) {
							_that.searchResult = res.data.stores;
							_that.callPropertyModal = true;
							return;
						}
						uni.showToast({
							title: "服务器异常了",
							icon: 'none',
							duration: 2000
						});
					},
					fail: function(e) {
						uni.showToast({
							title: "服务器异常了",
							icon: 'none',
							duration: 2000
						});
					}
				});
			
			},
			_formatList: function(arr, keyword) {
				let newArr1 = [];
				let tempArr = [];
				let reg = /\b(\w)|\s(\w)/g;
				let k = 0;
				let firstWord = arr[0][keyword]; //获取第一个分类字母
				arr.map((v) => {
					if (firstWord == v[keyword]) {
						tempArr.push(v);
						newArr1[k] = {
							initials: firstWord,
							staffs: tempArr
						}
					} else {
						//这里循环到这表示已经第二个字母了
						firstWord = v[keyword]; //设置第二字母
						tempArr = []; //把之前的清除掉
						tempArr.push(v); //添加第一个
						newArr1[++k] = { //自增
							initials: firstWord,
							staffs: tempArr
						}
					}
				});
				return newArr1;
			},
			_doCall: function(data) {
				uni.makePhoneCall({
					// 手机号
					phoneNumber: data.tel,
			
					// 成功回调
					success: (res) => {
						console.log('调用成功!')
					},
			
					// 失败回调
					fail: (res) => {
						console.log('调用失败!')
					}
			
				});
			},
			// 搜索城市
			searchCity() {
				// if (!this.inputVal) {
				// 	this.tui.toast("请输入搜索关键词")
				// 	return
				// }
				let result = []
				this._loadStaffInfo();
				this.inputShowed = true;
				// this.searchResult = result;
			},
			// 选择城市
			selectCity(e) {
				//返回并刷新上一页面
			},
			touchStart(e) {
				this.touchmove = true
				let pageY = e.touches[0].pageY
				let index = Math.floor((pageY - this.titleHeight) / this.indexBarItemHeight)
				let item = this.lists[index]
				if (item) {
					this.scrollViewId = item.letter;
					this.touchmoveIndex = index;
				}
			},
			touchMove(e) {
				let pageY = e.touches[0].pageY;
				let index = Math.floor((pageY - this.titleHeight) / this.indexBarItemHeight)
				let item = this.lists[index]
				if (item) {
					this.scrollViewId = item.letter;
					this.touchmoveIndex = index;
				}
			},
			touchEnd() {
				this.touchmove = false;
				this.touchmoveIndex = -1
			},
			touchCancel() {
				this.touchmove = false;
				this.touchmoveIndex = -1
			}
		}
	}
</script>

<style>
	page {
		height: 100%;
		overflow: hidden;
	}

	.page {
		height: 100%;
		overflow: hidden;
	}

	.scrollList {
		flex: 1;
	}

	.search-bar {
		display: flex;
		align-items: center;
		position: relative;
		padding: 30upx 30upx 30upx;
		background-color: #fff;
		/* box-shadow: 0 2px 2px #ccc;*/
	}

	.search-bar-form {
		flex: 1;
		position: relative;
		border-radius: 32upx;
		background: #f2f5f7;
	}

	.search-bar-box {
		display: flex;
		align-items: center;
		position: relative;
		padding-left: 20upx;
		padding-right: 20upx;
		height: 64upx;
		z-index: 1;
	}

	.search-bar-input {
		line-height: normal;
		width: 100%;
		padding-left: 20upx;
		font-size: 30upx;
		color: #333;
	}

	.phcolor {
		font-size: 30upx;
	}

	.icon-clear {
		height: 38upx;
	}

	.icon-search {
		position: relative;
		height: 26upx;
		margin-right: 20upx;
		font-size: inherit;
	}

	.cancel-btn {
		padding-left: 30upx;
	}

	.btn-primary {
		height: 62upx;
		width: 122upx;
		font-size: 30upx;
		line-height: 62upx;
		margin-left: 30upx;
	}

	.search-result::before {
		display: none;
	}

	.search-result::after {
		display: none;
	}

	.tui-list-cell {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		width: 100%;
	}

	.tui-list-cell-hover {
		background-color: #eee !important;
	}

	.tui-list-cell-navigate {
		width: 100%;
		position: relative;
		padding: 25upx 0 25upx 30upx;
		font-size: 28upx;
		color: #333;
		display: flex;
		align-items: center;
	}

	.tui-list-cell-navigate::after {
		content: '';
		position: absolute;
		border-bottom: 1upx solid #eaeef1;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
		bottom: 0;
		right: 0;
		left: 30upx;
	}

	.img {
		width: 120upx;
		height: 120upx;
		border-radius: 50%;
	}
	.img2 {
		width: 80upx;
		height: 80upx;
		border-radius: 50%;
	}

	.content {
		width: 84%;
	}

	.title {
		width: 80%;
		font-size: 34upx;
		padding-bottom: 10upx;
		color: #333;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis
	}

	.sub-title {
		width: 80%;
		color: #7A7A7A;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		font-size: 28upx;
		padding:0;
	}

	.tui-list {
		background-color: #fff;
		position: relative;
		width: 100%;
		display: flex;
		flex-direction: column;
		padding-bottom: env(safe-area-inset-bottom);
	}

	.tui-list-cell-divider {
		height: 66upx;
		padding-left: 30upx;
		font-size: 26upx;
		color: #999;
		background: #f2f5f7;
		padding: 0 30upx;
		display: flex;
		align-items: center;
	}

	.tui-indexed-list-bar {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: flex-start;
		z-index: 9999;
		position: absolute;
		top: 132upx;
		right: 0;
		padding-right: 10upx;
		width: 44upx;
	}

	.tui-indexed-list-text {
		font-size: 22upx;
	}

	.tui-indexed-list-bar.active {
		background-color: rgb(200, 200, 200);
	}

	.tui-indexed-list-alert {
		position: absolute;
		z-index: 20;
		width: 120upx;
		height: 120upx;
		right: 90upx;
		top: 50%;
		margin-top: -60upx;
		border-radius: 16upx;
		text-align: center;
		line-height: 120upx;
		font-size: 50upx;
		color: #fff;
		background-color: rgba(0, 0, 0, 0.5);
	}
	.btn-primary{
		background: #e41f19;
	}
</style>
